package com.nbsaas.boot.shopArticle.data.entity;

import com.nbsaas.boot.code.annotation.*;
import com.nbsaas.boot.jpa.data.entity.CatalogEntity;
import com.nbsaas.boot.rest.filter.Operator;
import com.nbsaas.boot.shop.data.entity.Shop;
import lombok.Data;
import org.hibernate.annotations.Comment;

import javax.persistence.*;
import java.io.Serializable;
import java.util.List;

/**
 * 文章分类
 *
 * @author 年高
 */

@FormAnnotation(model = "资讯分类")
@CatalogClass
@Data
@Entity
@Table(name = "bs_shop_article_catalog")
public class ShopArticleCatalog extends CatalogEntity {


	@SearchItem(label = "商家", name = "shop", key = "shop.id", operator = Operator.eq, classType = Long.class)
	@Comment("商家id")
	@JoinColumn(name = "shop_id")
	@FieldConvert
	@FieldName
	@ManyToOne(fetch = FetchType.LAZY)
	private Shop shop;

	/**
	 * 父节点
	 */
	@FieldConvert
	@ManyToOne(fetch = FetchType.LAZY)
	private ShopArticleCatalog parent;

	/**
	 * 数量
	 */
	private Long amount;

	@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
	private List<ShopArticleCatalog> children;


	@Override
	public Serializable getParentId() {
		if (parent != null) {
			return parent.getId();
		}
		return null;
	}
}
